package one.stand.service.impl;

import com.github.vioao.wechat.api.component.wxa.CodeTemplateApi;
import com.github.vioao.wechat.bean.response.BaseResponse;
import com.github.vioao.wechat.bean.response.component.wxa.GetCodeTemplateListResponse;
import com.github.vioao.wechat.bean.response.component.wxa.GetDraftCodeTemplateListResponse;
import com.jmp.base.dto.WechatComponentTokenResponse;
import com.jmp.feign.WechatFeign;
import one.stand.model.ResultModel;
import one.stand.service.CodeTemplateService;
import one.stand.util.AssertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author: QIK
 * @CreateDate: 2019/5/14 11:56
 */
@Service
public class CodeTemplateServiceImpl implements CodeTemplateService {
    @Autowired
    private WechatFeign wechatFeign;

    @Override
    public GetDraftCodeTemplateListResponse getTemplateDraftList() {
        ResultModel<WechatComponentTokenResponse> componentTokenResponseResultModel = wechatFeign.componentToken();
        AssertUtil.isTrue(componentTokenResponseResultModel);
        String token = componentTokenResponseResultModel.getData().getToken();
        return CodeTemplateApi.getTemplateDraftList(token);
    }

    @Override
    public GetCodeTemplateListResponse addTotemplate(String draftId) {
        ResultModel<WechatComponentTokenResponse> componentTokenResponseResultModel = wechatFeign.componentToken();
        AssertUtil.isTrue(componentTokenResponseResultModel);
        String token = componentTokenResponseResultModel.getData().getToken();
        GetCodeTemplateListResponse response = CodeTemplateApi.addTotemplate(token, draftId);
        return response;
    }

    @Override
    public GetCodeTemplateListResponse getTemplateList() {
        ResultModel<WechatComponentTokenResponse> componentTokenResponseResultModel = wechatFeign.componentToken();
        AssertUtil.isTrue(componentTokenResponseResultModel);
        String token = componentTokenResponseResultModel.getData().getToken();
        return CodeTemplateApi.getTemplateList(token);
    }


    @Override
    public BaseResponse delTemplate(String templateId) {
        ResultModel<WechatComponentTokenResponse> componentTokenResponseResultModel = wechatFeign.componentToken();
        AssertUtil.isTrue(componentTokenResponseResultModel);
        String token = componentTokenResponseResultModel.getData().getToken();
        return CodeTemplateApi.delTotemplate(token, templateId);
    }
}
